package springbook.controller;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springbook.model.*;
import springbook.service.BookService;

import java.util.List;

import static springbook.constant.Constans.USER_SESSION_KEY;

@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {
    @Autowired
    private BookService bookService;


    //用于展示某一页书籍
    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(RequestPage requestPage, HttpSession session) {
        log.info("当前页是："+requestPage.getOffset());
        //如果用户未登录
//        UserInfo userInfo = (UserInfo) session.getAttribute(USER_SESSION_KEY);
//        if(userInfo==null) {
//            return Result.noLogin();
//        }
        PageResult<BookInfo> result =  bookService.getListByPage(requestPage);
        return Result.success(result);
    }

    @RequestMapping("/addBook")
    public Boolean addBook( BookInfo bookInfo) {
        log.info("新增图书");
        //1.校验图书对象信息
        if( !StringUtils.hasLength(bookInfo.getBookName())
            || !StringUtils.hasLength(bookInfo.getAuthor())
            || bookInfo.getCount()==null
            || bookInfo.getPrice()==null
            || !StringUtils.hasLength(bookInfo.getPublish())
            ) {
            return false;
        }
        //2.验证该书籍是否已经存在
        if(bookService.queryBookExist(bookInfo)) return false;
        Integer result =  bookService.addBook(bookInfo);
        if(result==0) return false;
        return true;
    }
    //查询某本书（用于修改）
    @RequestMapping("/getBookInfo")
    public BookInfo getBookInfo(Integer bookId) {
        return bookService.queryBookById(bookId);
    }
    //修改某一本书
    @RequestMapping("/updateBook")
    public Boolean updateBook(BookInfo bookInfo) {
        log.info("修改："+bookInfo);
        if(bookInfo==null) return false;
        if(bookInfo.getId()==null) {
            log.warn("Id不能为空");
            return false;
        }
        Integer result = bookService.updateBook(bookInfo);
        try {
            if(result>0) return true;
            return false;
        }catch (Exception e) {
            log.error("修改错误");
            return false;
        }

    }
    @RequestMapping("/deleteBook")
    public Boolean deleteBook(Integer bookId) {
        log.info("删除："+bookId);
        if(bookId==null) {
            log.warn("Id不能为空");
            return false;
        }
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(0);
        bookService.updateBook(bookInfo);
        return true;
    }
    @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids) {
        log.info("ids:"+ids);
        if(ids==null || ids.size()==0) return false;
        Integer result = bookService.batchDelete(ids);
        try {
            if(result>0) return true;
            return false;
        }catch (Exception e) {
            log.error("批量删除错误");
            return false;
        }
    }

}
